package com.iseastar.guojiang.transfer;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;

import com.baidu.location.BDLocation;
import com.iseastar.BaseActivity2;
import com.iseastar.guojiang.app.Intents;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.dialog.DialogMgr;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ParcelTransferPointBean;
import com.iseastar.guojiang.model.ReqResult;
import com.iseastar.guojiang.newcabinet.TaskMainDetailActivity;
import com.shouhuobao.station.R;

import droid.frame.view.xlist.SListView;

/**
 * Created by joker on 2018/1/29.
 * 火箭侠------包裹交接点列表
 */
public class ParcelTransferListActivity extends BaseActivity2 {

    private SListView mListView;
    private ParcelTransferListAdapter mAdapter = null;
    private ParcelTransferPointBean item = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showLoadingDialog("正在定位");
        Intents.startBaiduLocation(getContext(), null);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.transfer_parcel_list);
        super.findViewById();
        getAppTitle().setCommonTitle("包裹交接点列表");
        mListView = (SListView) findViewById(R.id.listview);
        mAdapter = new ParcelTransferListAdapter(null, getContext());
        mListView.setAdapter(mAdapter);
        mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                showLoadingDialog("正在定位");
                Intents.startBaiduLocation(getContext(), null);
            }
        });
    }

    @Override
    public boolean handleMessage(final Message msg) {
        switch (msg.what) {
            case MsgID.location_baidu:
                cancelLoadingDialog();
                BDLocation bdLocation = (BDLocation) msg.obj;
                if (bdLocation == null) {
                    onRefreshComplete();
                    showToast("定位失败");
                } else {
                    showLoadingDialog(null);
                    AppHttp.getInstance().courierTransferList(bdLocation.getLatitude(), bdLocation.getLongitude());
                }
                return false;
            //包裹交接列表返回数据
            case MsgID.courier_tansfer_list:
                cancelLoadingDialog();
                onRefreshComplete();
                final ReqResult<ParcelTransferPointBean> reqResult = JSON.parseList(msg.obj, ParcelTransferPointBean.class);
                if (checkLoginStatus(reqResult)) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (mAdapter == null) {
                                mAdapter = new ParcelTransferListAdapter(reqResult.getResultList(), getContext());
                                mListView.setAdapter(mAdapter);
                            } else {
                                mAdapter.setItems(reqResult.getResultList(), true);
                            }
                        }
                    });
                } else {
                    showToast(reqResult.getMessage());
                }
                return true;
            //设置为默认仓
            case MsgID.courier_set_default_tansfer:
                cancelLoadingDialog();
                ReqResult<?> result = JSON.parser(msg.obj);
                if (checkLoginStatus(result)) {
                    showToast("设置成功!");
                    Intent intent = new Intent(getContext(), TaskMainDetailActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("item", item);
                    bundle.putInt("type", 2);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    finish();
                } else {
                    showToast(result.getMessage());
                }
                return true;
            default:
                return super.handleMessage(msg);
        }
    }

    /**
     * 导航前往
     *
     * @param item
     */
    public void navigation(ParcelTransferPointBean item) {
        DialogMgr.showSelectMapToNavigation(getContext(), mListView, item.getBdLatitude(), item.getBdLongitude(), item.getGdLatitude(), item.getGdLongitude());
    }

    /**
     * 设为默认交接点
     *
     * @param item
     */
    public void setDefaultTransferPoint(int position, ParcelTransferPointBean item) {
        if (item == null) {
            return;
        }
        this.item = item;
        DialogMgr.setDefaultTransferDialog(getContext());
    }

    public void setDefaultTransfer() {
        if (item == null) {
            return;
        }
        showLoadingDialog(null);
        AppHttp.getInstance().courierSetDefaultTransfer(item.getId());
    }
}
